﻿import re
import time
from datetime import datetime

from cx_Oracle import IntegrityError

from crawl.utils import update_score, save_review, review_split, close_db, newReview, log_info, logger, conn, c, \
    SKU_DETAIL_ID, max_date, sql_commit
import locale
from crawl.unchange.amazon.amazon_review import AmazonReview


class AmazonNewReview(AmazonReview):

    def comment_data(self, html):
        comments_divs = html.xpath("//div[@id='cm_cr-review_list']/div[@data-hook='review']")
        if len(comments_divs) < 1:
            log_info("{}({})获取评论主标签失败".format(self.name, self.SKU_ID))
            return True
        n = 0
        for div in comments_divs:
            try:
                comment_dict = {}
                comment_dict['num'] = self.comment_num
                # 评论时间
                comment_time = div.xpath(".//span[@data-hook='review-date']/text()")
                comment_dict['comment_time'] = comment_time[0] if len(comment_time) > 0 else ""
                if comment_dict['comment_time']:
                    flag = comment_dict['comment_time']
                    timeList = flag.split(" ")[-3:]
                    # 拼接时间字符串
                    if re.search(r".com", self.amazon_url):
                        dataStr = timeList[0].replace(",", "") + "/" + timeList[1].replace(",", "") + "/" + timeList[2].replace(",", "")
                        locale.setlocale(locale.LC_ALL, 'en')
                        re_date = datetime.strptime(dataStr, "%B/%d/%Y")
                    elif re.search(r".co.uk", self.amazon_url):
                        dataStr = timeList[1].replace(",", "") + "/" + timeList[0].replace(".", "").replace(",", "") + "/" + timeList[2].replace(",", "")
                        locale.setlocale(locale.LC_ALL, 'en')
                        re_date = datetime.strptime(dataStr, "%B/%d/%Y")
                    elif re.search(r".de", self.amazon_url):
                        dataStr = timeList[1].replace(",", "") + "/" + timeList[0].replace(".", "").replace(",", "") + "/" + timeList[2].replace(",", "")
                        # 设置本地时间格式为德语
                        locale.setlocale(locale.LC_ALL, 'de')
                        re_date = datetime.strptime(dataStr, "%B/%d/%Y")
                    elif re.search(r".fr", self.amazon_url):
                        dataStr = timeList[1].replace(",", "") + "/" + timeList[0].replace(".", "").replace(",", "") + "/" + timeList[2].replace(",", "")
                        # 设置本地时间格式为法语
                        locale.setlocale(locale.LC_ALL, 'fr')
                        re_date = datetime.strptime(dataStr, "%B/%d/%Y")
                    elif re.search(r".co.jp", self.amazon_url):
                        dataStr = re.search(r"(\d+)\D+(\d+)\D+(\d+)", flag)
                        dataStr = dataStr.group(1) + "/" + dataStr.group(2) + "/" + dataStr.group(3)
                        re_date = datetime.strptime(dataStr, "%Y/%m/%d")
                    else:
                        return True
                    locale.setlocale(locale.LC_ALL, "C")
                    REVIEW_DATE = re_date.strftime('%Y/%m/%d')
                    # 查询数据库评论最晚日期
                    if newReview(self.max_d, re_date):
                        n += 1
                        if n < 3:
                            continue
                        else:
                            return True
                else:
                    REVIEW_DATE = datetime.now().strftime('%Y/%m/%d')
                # 评论ID
                if len(div.xpath("./@id")) > 0:
                    REVIEW_ID = self.SKU_DETAIL_ID + "_" + div.xpath("./@id")[0]
                else:
                    continue
                # 评论星级
                score = div.xpath(".//span[@class='a-icon-alt']/text()")[0].split(" ")[0].replace(",", ".")
                score = float(score)
                # 评论标题
                comment_title = div.xpath(
                    ".//a[@data-hook='review-title']/span/text()")
                comment_dict['comment_title'] = comment_title[0] if len(comment_title) > 0 else ""
                # 评论用户
                comment_user = div.xpath(
                    ".//span[@class='a-profile-name']/text()")
                comment_dict['comment_user'] = comment_user[0] if len(comment_user) > 0 else ""
                # 评论内容
                comment_dict['comment_content'] = " ".join(div.xpath(".//span[@data-hook='review-body']/span/text()"))
                self.comments_list.append(comment_dict)
                REVIEW_TEXT = comment_dict['comment_content']
                # 评论内容分拆
                REVIEW_TEXT1, REVIEW_TEXT2, REVIEW_TEXT3, REVIEW_TEXT4, REVIEW_TEXT5 = review_split(REVIEW_TEXT)
                REVIEW_TEXT5 += "  from_amazon"
            except:
                logger(self.name, self.SKU_ID, "提取内容失败")
                continue
            # 保存到数据库
            sql = save_review(REVIEW_ID, self.SKU_ID, score, comment_dict['comment_user'], comment_dict['comment_title'], REVIEW_TEXT1, REVIEW_TEXT2, REVIEW_TEXT3, REVIEW_TEXT4, REVIEW_DATE, REVIEW_TEXT5, self.SKU_DETAIL_ID)
            try:
                c.execute(sql)
                conn.commit()
                self.comment_num += 1
            except IntegrityError:
                conn.rollback()
            except UnicodeEncodeError:
                conn.rollback()
            except Exception as e:
                logger(self.name, self.SKU_ID, e)
                conn.rollback()

    def get_score(self, html, sku_id):
        self.max_d = max_date(self.SKU_DETAIL_ID)
        try:
            total_score = html.xpath("//span[@data-hook='rating-out-of-text']/text()")
            score = re.match(r"(\S+)", total_score[0].replace("，", " ")).group(1).replace(",", ".")
            rev_counts = html.xpath("//span[@class='a-size-base a-color-secondary']/text()")
            rev_counts = re.search(r"(\d+)", rev_counts[0].replace(",", "").replace(".", "").replace(" ", "").replace(" ", "")).group(1)
        except Exception as e:
            logger(self.name, sku_id, "无总评分/总数数据")
            return
        try:
            five_star = html.xpath("//tr[@class='a-histogram-row'][{}]//span[@class='a-size-base']/a[@class='a-link-normal']/@title".format(1))[0]
            five_star = re.search(r'(\d+)%', five_star).group()
        except:
            five_star = "0%"
        try:
            four_star = html.xpath("//tr[@class='a-histogram-row'][{}]//span[@class='a-size-base']/a[@class='a-link-normal']/@title".format(2))[0]
            four_star = re.search(r'(\d+)%', four_star).group()
        except:
            four_star = "0%"
        try:
            three_star = html.xpath("//tr[@class='a-histogram-row'][{}]//span[@class='a-size-base']/a[@class='a-link-normal']/@title".format(3))[0]
            three_star = re.search(r'(\d+)%', three_star).group()
        except:
            three_star = "0%"
        try:
            two_star = html.xpath("//tr[@class='a-histogram-row'][{}]//span[@class='a-size-base']/a[@class='a-link-normal']/@title".format(4))[0]
            two_star = re.search(r'(\d+)%', two_star).group()
        except:
            two_star = "0%"
        try:
            one_star = html.xpath("//tr[@class='a-histogram-row'][{}]//span[@class='a-size-base']/a[@class='a-link-normal']/@title".format(5))[0]
            one_star = re.search(r'(\d+)%', one_star).group()
        except:
            one_star = "0%"
        update_score(score, self.SKU_ID, self.name, self.SKU_DETAIL_ID, conn, rev_counts, five_star, four_star, three_star, two_star, one_star)


def main(urls):
    if len(urls) < 1 or isinstance(urls, list) is False:
        log_info("Amazon,无url信息或传入参数格式不是列表")
        return True
    start = time.time()
    for url in urls:
        amazon = AmazonNewReview()
        amazon.run(url)
    end = time.time()
    log_info("Amazon_end,耗时%s秒" % (end - start))
